<template>
  <!--    搜索头部区域-->
  <div style="padding: 10px; height: 50px; line-height: 50px; display: flex">
    <el-input
      v-model.trim="search"
      placeholder="请输入DockerID"
      style="width: 20%"
      clearable
    />
    <el-button
      type="primary"
      color="#fab6b6"
      style="margin-left: 10px"
      @click="find"
      >查询</el-button
    >
    <div style="flex: 1"></div>
    <div
        style="
        width: 300px;
        display: flex;
        justify-content: right;
        align-items: center;
      "
    >
      <el-button
          type="primary"
          style="margin-left: 15px"
          @click="toDashBoard"
      >Kubernetes DashBoard &nbsp &nbsp
        <el-icon><ArrowRightBold /></el-icon>
      </el-button>
    </div>
  </div>
  <div style="padding: 10px">
    <el-dialog v-model="deleteDialogVisible" title="注销确认" width="30%">
      <el-form :model="deleteForm" label-width="100px">
        <el-form-item label="Docker名" >
          <el-input v-model.trim="deleteForm.podName" disabled></el-input>
        </el-form-item>
        <el-form-item label="验证用户密码">
          <el-input
              prefix-icon="Lock"
              v-model.trim="deleteForm.password"
              show-password
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span>
          <el-button type="primary" color="#fab6b6" @click="deletePod"
          >注销</el-button
          >
        </span>
      </template>
    </el-dialog>
    <el-table :data="tableData" border="border">
      <el-table-column prop="userID" label="所属用户ID" width="120px" />
      <el-table-column prop="id" label="DockerID" width="120px" />
      <el-table-column prop="name" label="Docker名" />
      <el-table-column prop="cpu" label="cpu核心数" />
      <el-table-column prop="memory" label="内存(MB)" />
      <el-table-column prop="imageOS" label="操作系统" :formatter=formatOS />
      <el-table-column prop="imageVersion" label="操作系统版本" :formatter=formatVersion />
      <el-table-column prop="nodePort" label="连接端口" />
      <el-table-column prop="expirationTime" label="到期时间" :formatter=formatTime />
      <el-table-column label="操作" width="100px" align="center">
        <template #default="scope">
          <el-button  @click="handleDelete(scope.row)" color="#fab6b6">注销</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
        v-model:currentPage="currentPage"
        :page-sizes="[5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "../../untils/request";

export default {
  name: "allDockerManage",

  data() {
    return {
      deleteDialogVisible:false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      form: {}, //某行数据
      addForm: {}, //新增Docker
      reletForm: {}, //续租
      deleteForm: {}, //注销
      search: "",
    };
  },
  created() {
    this.load();
  },
  methods: {
    toDashBoard(){
      window.open("https://192.168.88.137:31224");
    },
    find() {
      request
        .get("/user/findUserByID", {
          params: {
            pageNum: this.currentPage,
            pageSize: this.pageSize,
            search: this.search,
          },
        })
        .then((res) => {
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
    },
    deletePod(){
      this.$confirm("Docker注销不可撤销？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        request.get("/pod/delete", {
          params: {
            dockerID: this.deleteForm.dockerID,
            password: this.deleteForm.password,
          },
        }).then((res) => {
          if (res.code === 0) {
            this.$message({
              type: "success",
              message: "注销成功",
            });
            this.deleteDialogVisible = false;
            this.load();
          } else {
            this.$message({
              type: "error",
              message: res.message,
            });
          }
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消",
        });
      });
    },
    load() {
      request.get("/pod/viewActivePod", {
          params: {
            pageNum: this.currentPage,
            pageSize: this.pageSize,
          },
        })
        .then((res) => {
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
    },
    formatOS(row){
      if (row.imageOS === "ubuntu") {
        return "Ubuntu";
      }
      if (row.imageOS === "centos") {
        return "CentOS";
      }
    },
    formatVersion(row){
      if (row.imageOS === "ubuntu" && row.imageVersion === "C") {
        return "22.04（GCC，G++捆绑包）";
      }
      if (row.imageOS === "ubuntu" && row.imageVersion === "Python") {
        return "22.04（Python捆绑包）";
      }
      return row.imageVersion;
    },
    formatTime(row){
      return row.expirationTime.split('T')[0];
    },
    handleDelete(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.deleteDialogVisible = true;
      this.deleteForm.podName=this.form.name;
      this.deleteForm.dockerID=this.form.id;
    },
    handleSizeChange(pageSize) {
      //改变当前每页的个数触发
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      //改变当前页码触发
      this.currentPage = pageNum;
      this.load();
    },

  },
};
</script>

<style scoped lang="less">
/deep/ .el-input {
  width: 85%;
}

/deep/ .el-dialog {
  background: #fcf9f4;
}

/deep/.el-form-item {
  margin-bottom: 30px;
}
</style>
<style lang="less">
.el-message-box {
  background: #fcf9f4;
}
</style>
